import axios from 'axios'
const service = axios.create({
   // baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000
})

// request 拦截器
service.interceptors.request.use(
    config => {
        // 在这里可以设置请求头、请求参数等return config
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
       const { data } = response
        if (response.status !== 200) {
            console.error('Error:', data)
            return Promise.reject(newError(data || 'Error'))
        } else {
            return data
        }
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

// 封装具体的请求方法: 四种请求类型不和下面的3和4标题内容放在一起
export const get = (url, params) => {
    return service .get(url, { params });
};

export const post = (url, data) => {
    return service .post(url, data);
};

export const put = (url, data) => {
    return service .put(url, data);
};

export const deleted= (url, data) => {
    return service .delete(url, data);
};

export default service